package com.zoe.wan.android.example.fragment.personal

import android.app.Application
import com.zoe.wan.android.example.Constants
import androidx.databinding.ObservableField
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.zoe.wan.android.example.repositry.Repository
import com.zoe.wan.base.BaseViewModel
import kotlinx.coroutines.launch

class FragPersonalViewModel(application: Application) : BaseViewModel(application) {

    val userName = ObservableField<String>()

    var showLogBtn = ObservableField<Boolean>()

    init {

        val name = SPUtils.getInstance().getString(Constants.SP_USER_NAME)

        if (name.isNullOrEmpty()) {
            userName.set("未登录")
            showLogBtn.set(false)
        } else {
            userName.set(name)
            showLogBtn.set(true)
        }

    }

    fun logout(){
        viewModelScope.launch {
            val status = Repository.logout()
            if (status == true){
                SPUtils.getInstance().clear()
                userName.set("未登录")
                showLogBtn.set(false)

                ToastUtils.showLong("退出成功")
            }else{
                ToastUtils.showLong("退出失败")
            }
        }
    }
}